﻿using TransCend.Util;
using TransCend.Util.Page;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace TransCend.Model
{
    /// <summary>
    /// 基础Model，所以model继承
    /// </summary>
    public class BaseModel : IDictionary
    {
        private IDictionary m = null;

        private const string SORT = "sort";
        private const string ORDER = "order";
        //排序语名字段
        private const string ORDER_BY = "ORDER_BY";
        private const string PAGESTART = "pagestart";
        private const string PAGE = "page";
        private const string ROWS = "rows";

        protected const String ORDER_ASC = "asc";
        protected const String ORDER_DESC = "desc";

        public BaseModel()
        {
            m = new Dictionary<object, object>();
        }

        public BaseModel(int capacity)
        {
            m = new Dictionary<object, object>(capacity);
        }

        public BaseModel(IDictionary m)
        {
            this.m = m;
        }

        public BaseModel(SerializationInfo info, StreamingContext context)
        {

        }

        public void Add(object key, object value)
        {
            m.Add(key, value);
        }

        public void Clear()
        {
            m.Clear();
        }

        public bool Contains(object key)
        {
            return m.Contains(key);
        }

        public IDictionaryEnumerator GetEnumerator()
        {
            return m.GetEnumerator();
        }

        public bool IsFixedSize
        {
            get { return m.IsFixedSize; }
        }

        public bool IsReadOnly
        {
            get { return m.IsReadOnly; }
        }

        public ICollection Keys
        {
            get { return m.Keys; }
        }

        public void Remove(object key)
        {
            m.Remove(key);
        }

        public ICollection Values
        {
            get { return m.Values; }
        }

        public object this[object key]
        {
            get
            {
                if (key.Equals(PAGE))
                {
                    object page;
                    if (this.ContainsKey(key)) page = m[PAGE];
                    else page = "1";
                    return page != null && page.ToString() != "" ? int.Parse(page.ToString()) : 1;
                }
                if (key.Equals(ROWS))
                {
                    object rows;
                    if (this.ContainsKey(key)) rows = m[ROWS];
                    else rows = SimplePage.DEF_COUNT;
                    return rows != null && rows.ToString() != "" ? int.Parse(rows.ToString()) : SimplePage.DEF_COUNT;
                }
                if (key.Equals(ORDER_BY))
                {
                    return GetOrderBy();
                }
                return m[key];
            }
            set
            {
                //m[key] = value is DateTime ? ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss") : value;
                m[key] = value;
            }
        }

        public void CopyTo(Array array, int index)
        {
            m.CopyTo(array, index);
        }

        public int Count
        {
            get { return m.Count; }
        }

        public bool IsSynchronized
        {
            get { return m.IsSynchronized; }
        }

        public object SyncRoot
        {
            get { return m.SyncRoot; }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return m.GetEnumerator();
        }

        public void PutAll(IDictionary dict)
        {
            foreach (object key in dict.Keys)
            {
                this[key] = dict[key];
            }
        }

        public bool ContainsKey(object key)
        {
            return this.Contains(key);
        }

        private string GetOrderBy()
        {
            string sort = GetString(SORT);
            if (!string.IsNullOrEmpty(sort))
            {
                StringUtils.AssertSQLField(sort);
                StringBuilder sb = new StringBuilder();
                sb.Append(" ORDER BY ");
                String[] n = sort.Split(',');
                String[] no = null;
                string order = GetString(ORDER);
                if (!string.IsNullOrEmpty(order))
                {
                    StringUtils.AssertSQLField(order);
                    no = order.Split(',');
                }
                for (int i = 0; i < n.Length; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(",");
                    }
                    sb.Append(n[i]);
                    if (no != null && no.Length > i)
                    {
                        sb.Append(" ").Append(no[i]);
                    }
                }
                return sb.ToString();
            }
            return " ORDER BY 1";
        }

        public int Page
        {
            set { this[PAGE] = value; }
            get
            {
                int? page = GetInt(PAGE);
                return page.HasValue ? page.Value : 1;
            }
        }

        public int Rows
        {
            set { this[ROWS] = value; }
            get
            {
                int? rows = GetInt(ROWS);
                return rows.HasValue ? rows.Value : SimplePage.DEF_COUNT;
            }
        }

        public string Sort
        {
            get { return GetString(SORT); }
            set { this[SORT] = value; }
        }

        public string Order
        {
            get { return GetString(ORDER); }
            set { this[ORDER] = value; }
        }

        public string OrderBy
        {
            get { return GetString(ORDER_BY); }
        }

        private object GetObject(string key)
        {
            return this[key];
        }

        public string GetString(string key)
        {
            if (this.ContainsKey(key))
            {
                return this[key] == null ? "" : this[key].ToString();
            }
            return null;
        }

        public string GetNText(string key)
        {
            if (this.ContainsKey(key))
            {
                return this[key] == null ? "" : this[key].ToString();
            }
            return null;
        }

        public string GetChar(string key)
        {
            if (this.ContainsKey(key))
            {
                return this[key] == null ? "" : this[key].ToString();
            }
            return null;
        }

        public int? GetInt(string key)
        {
            if (this.ContainsKey(key))
            {
                if (this[key] == null || this[key].ToString() == "")
                {
                    return null;
                }
                return int.Parse(this[key].ToString());
            }
            return null;
        }

        public bool GetBool(string key)
        {
            if (this.ContainsKey(key))
            {
                return this[key] == null || this[key].ToString() == "" ? default(bool) : bool.Parse(this[key].ToString());
            }
            return false;
        }

        public bool GetBit(string key)
        {
            if (this.ContainsKey(key))
            {
                return this[key] == null || this[key].ToString() == "" ? default(bool) : bool.Parse(this[key].ToString());
            }
            return false;
        }

        public long? GetLong(string key)
        {
            if (this.ContainsKey(key))
            {
                if (this[key] == null || this[key].ToString() == "")
                {
                    return null;
                }
                return long.Parse(this[key].ToString());
            }
            return null;
        }

        public double? GetDouble(string key)
        {
            if (this.ContainsKey(key))
            {
                if (this[key] == null || this[key].ToString() == "")
                {
                    return null;
                }
                return double.Parse(this[key].ToString());
            }
            return null;
        }

        public decimal? GetDecimal(string key)
        {
            if (this.ContainsKey(key))
            {
                if (this[key] == null || this[key].ToString() == "")
                {
                    return null;
                }
                return decimal.Parse(this[key].ToString());
            }
            return null;
        }

        public DateTime? GetDateTime(string key)
        {
            if (this.ContainsKey(key))
            {
                if (this[key] == null || this[key].ToString() == "")
                {
                    return null;
                }
                if (this[key] is DateTime)
                {
                    return (DateTime)this[key];
                }
                return DateTime.Parse(this[key].ToString());
            }
            return null;
        }
    }
}
